﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;

namespace Turtle8
{
    public partial class SoldierQualifiedViewCard : UserControl
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da;
      
        public SoldierQualifiedViewCard()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            //Initialize the SqlDataAdapter object by specifying a Select command 
            //that retrieves data from the sample table.
            string selectStr = CompanyManager.getSqlStringFromFile(@"\SELECT\SoldierQualifiedViewCard.SELECT.sql");
            da = SqlManager.SqlDataAdapterFactory(selectStr);

            string updateStr = CompanyManager.getSqlStringFromFile(@"\UPDATE\SoldierQualifiedViewCard.UPDATE.sql");
            da.UpdateCommand = SqlManager.SqlCommandFactory(updateStr);

            ICollection<string> col = CompanyManager.parseSqlQueryParameters(updateStr);
            //adding parameters dynamically to the SqlCommand
            foreach (string item in col)
            {
                switch (item)
                {
                    case "armyID":
                        //add the primary key Parameter.
                        da.UpdateCommand.Parameters.Add(new SqlParameter("@armyID", SqlDbType.NVarChar));
                        da.UpdateCommand.Parameters["@armyID"].SourceVersion = DataRowVersion.Original;
                        da.UpdateCommand.Parameters["@armyID"].SourceColumn = "armyId";
                        break;
                    default:
                        da.UpdateCommand.Parameters.Add(new SqlParameter(item, SqlDbType.Date));
                        da.UpdateCommand.Parameters[item].SourceVersion = DataRowVersion.Current;
                        da.UpdateCommand.Parameters[item].SourceColumn = item;
                        break;
                }
            }

            refresh();
           
        }

        private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {      
            string str = (string)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;
            try
            {
               dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DateTime.ParseExact(str, "dd-MM-yyyy", null);
            }
            catch (Exception)
            {
                MessageBox.Show("dd-mm-yyyy :המידע שהוזן שגוי. התבנית הרצויה היא");
            }
        }


        public void refresh()
        {
            timer_Tick(this, EventArgs.Empty);
            //clean and thean populate the DataSet by running the Fill method of the SqlDataAdapter.
            dt.Clear();
            da.Fill(dt);
            dataGridView.DataSource = dt;

            //allowing the user to change just the other columns
            dataGridView.Columns["firstName"].ReadOnly = true;
            dataGridView.Columns["lastName"].ReadOnly = true;
            dataGridView.Columns["platoon"].ReadOnly = true;
            dataGridView.Columns["armyID"].ReadOnly = true;

            //formatting the display of the the DateTime Columns
            foreach (DataGridViewColumn item in dataGridView.Columns)
                if (!item.ReadOnly)
                {
                    dataGridView.Columns[item.Name].DefaultCellStyle.Format = "dd-MM-yyyy";
                    dataGridView.Columns[item.Name].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                }

            CompanyManager.setTableColumnHeadersHebrew(dataGridView.Columns);

            showLabelMessage("רענון בוצע בהצלחה");
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            labelMessage.Visible = false;
            timer.Stop();
        }


        private void showLabelMessage(string message)
        {
            labelMessage.Visible = true;
            labelMessage.Text = message;
            labelMessage.Location = new Point(this.splitContainer.Panel1.Bounds.Width / 2, 16);
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            da.Update(dt);
            showLabelMessage("עדכון בוצע בהצלחה");
            timer.Start();
        }


    }
}
